package cn.neusoft.yuejian307.Fragment;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

import cn.neusoft.yuejian307.R;
import cn.neusoft.yuejian307.adapter.TrainAdapter;
import cn.neusoft.yuejian307.banners.BannerView;
import cn.neusoft.yuejian307.bean.Train;
import cn.neusoft.yuejian307.listener.UserListener;
import cn.neusoft.yuejian307.model.UserModel;



public class FragmentOne extends Fragment implements UserListener<List<Train>>{
    View view;
    private List<View> viewList;
    BannerView bannerView;
    private int[] imgs = {R.drawable.one,R.drawable.two,R.drawable.three,R.drawable.four,R.drawable.five};
    private RecyclerView recyclerView_train;
    @Override
    public View onCreateView(LayoutInflater inflater , ViewGroup container , Bundle savedInstanceState)
    {
        view = inflater.inflate(R.layout.fragment_one, null) ;
        initBannerView();
        recyclerView_train = (RecyclerView)view.findViewById(R.id.recyclerView_train);
        recyclerView_train.setLayoutManager(new LinearLayoutManager(getActivity()));
        UserModel userModel = new UserModel();
        userModel.trainData(this,4);
        return view ;
    }
    private void initBannerView(){

        viewList = new ArrayList<View>();

        for (int i = 0; i < imgs.length; i++) {
            ImageView image = new ImageView(getActivity());
            image.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
            //设置显示格式
            image.setScaleType(ImageView.ScaleType.CENTER_CROP);
            image.setImageResource(imgs[i]);
            viewList.add(image);
        }
        bannerView = (BannerView) view.findViewById(R.id.banner);
        bannerView.startLoop(true);

        bannerView.setViewList(viewList);
       // bannerView.setTransformAnim(true);
    }


    @Override
    public void onResponse(List<Train> train) {
        TrainAdapter adapter = new TrainAdapter(getActivity(),  train);
        recyclerView_train.setAdapter(adapter);
    }

    @Override
    public void onFail(String msg) {
        Toast.makeText(getActivity(),"失败",Toast.LENGTH_LONG).show();
    }
}
